package com.wx.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.wx.dao.OrdersettingDao;
import com.wx.pojo.OrderSetting;
import com.wx.service.OrdersettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.*;

// 注册到dubbo中
@Service(interfaceClass = OrdersettingService.class)
//开启事务支持
@Transactional
public class OrdersettingServiceImp implements OrdersettingService {

    @Autowired
    private OrdersettingDao dao;
    @Override

    public void batchAddOerdersetting(List<OrderSetting> list) {
        for (OrderSetting orderSetting : list) {
            // 先判断是否存在
            Date date = orderSetting.getOrderDate();
           OrderSetting orderByDate = dao.findOrderByDate(date);
            if(orderByDate!=null){
                // 日期已经存在，需要覆盖
                orderSetting.setReservations(0);
                dao.updateOrder(orderSetting);
            }else {
                // 日期不存在新添加
                orderSetting.setReservations(0);
                dao.addOrder(orderSetting);
            }
        }


    }
    //    List<Map> findAllMessage(List list);
    @Override
    public   List<Map> findAllMessage(List list) {
        Date date1 = (Date) list.get(0);
        Date date2 = (Date) list.get(1);

        List<OrderSetting> allMessage = dao.findAllMessage(date2, date1);
        // 把OrderSetting类中的date转换成为数字
        LinkedList<Map> maps = new LinkedList<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        System.out.println(sdf.format(date1)+":"+sdf.format(date2));
        for (OrderSetting orderSetting : allMessage) {
            HashMap<String, Integer> map = new HashMap<>();
            Date orderDate = orderSetting.getOrderDate();
            String format = sdf.format(orderDate);
            // 对这个日期进行切割
            String[] split = format.split("-");
            // 获得对应的天数
            String day = split[2];
            // 转换位数字
            int intDay = Integer.parseInt(day);
            // 把数据塞进map集合中
            map.put("date",intDay);
            map.put("number",orderSetting.getNumber());
            map.put("reservations",orderSetting.getReservations());
            // 把集合放入list集合中
            maps.add(map);
        }
        return maps;
    }

    @Override
    public void updateOrderByDate(OrderSetting orderSetting) {
        dao.updateOrderByDate(orderSetting);
    }


}
